public class binarySearch {
    //二分查找
    public static void main(String[] args) {
        int[] arr={1,2,3,4,5,6};
        if(binarySearch(arr,6)==-1){
            System.out.println("没找到");
        }else{
            System.out.println("找到了，下标为:"+binarySearch(arr,6));
        }
    }
    public static int binarySearch(int[] arr,int target){

       int left =0;
       int right=arr.length-1;

       while(left<=right){
           int mid=(right+left)/2;
           if(target>arr[mid]){
               left=mid+1;
           }else if(target<arr[mid]){
               right=mid-1;
           }else{
              return mid;
           }
       }
       return -1;
    }
}
